package com.example.tivi.ui;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;

import com.example.tivi.R;
import com.example.tivi.connection.DataLoader;
import com.google.analytics.tracking.android.EasyTracker;

public class SplashActivity extends FragmentActivity implements
		LoaderCallbacks<Boolean> {
	private static final String TAG = "SplashActivity";
	private static final int LOADER_ID_DATA = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_splash);
		getSupportLoaderManager().restartLoader(LOADER_ID_DATA, null, this)
				.forceLoad();
	}

	@Override
	protected void onStart() {
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	public Loader<Boolean> onCreateLoader(int loaderID, Bundle bundle) {
		return new DataLoader(getApplicationContext());
	}

	@Override
	public void onLoadFinished(Loader<Boolean> loader, Boolean value) {
		if (value) {
			startActivity(new Intent(this, MainActivity.class));
			finish();
		} else {
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle(R.string.notice);
			builder.setMessage(R.string.can_not_connect);
			builder.setPositiveButton(R.string.ok,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							finish();
						}
					});
			builder.show();
		}
	}

	@Override
	public void onLoaderReset(Loader<Boolean> loader) {

	}
}
